<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="120px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-divider content-position="left"><span>1、【风险应对】相关信息</span></el-divider>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险名称" prop="riskname" :rules="rules.validateRequire">
            <el-input v-model="model.riskname" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmRiskIdentifyRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="应对效果" prop="responseeffect" :rules="rules.validateRequire">
            <el-select v-model="model.responseeffect" filterable clearable>
              <el-option v-for="item in codeModel.responseeffectList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="应对措施" prop="responsemeasures" :rules="rules.validateRequire">
            <el-input v-model="model.responsemeasures" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="应对人" prop="responseusername" :rules="rules.validateRequire">
            <el-input v-model="model.responseusername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmTeamRefDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="应对人工号" prop="responseusercode">
            <el-input v-model="model.responseusercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="应对时间" prop="responsetime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.responsetime" type="date" placeholder="应对时间" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="确认人" prop="confirmusername" :required="true" :rules="rules.validateRequire">
            <el-input v-model="model.confirmusername" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmTeamRefConfirmDialog" v-if="!disableEditSubmit && !disableSave"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="确认人工号" prop="confirmusercode">
            <el-input v-model="model.confirmusercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-show="model.state == '9'">
          <el-form-item label="确认时间" prop="confirmtime">
            <el-date-picker v-model="model.confirmtime" type="date" :disabled="true" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】相关信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="deptname">
                <tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.deptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="orgname">
                <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人" prop="username">
                <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="usercode">
                <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate" :rules="rules.validateRequire">
                <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="datetime" placeholder="提交时间" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="审批流程" prop="flowid">
                <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="state">
                <el-input v-model="model.dispstate" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    <tl-approval-history ref="history" sourceType="PmRiskResponses" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit" v-if="uiOperateState<=2">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-attachment sourceType="PmRiskResponses" :sourceId="model.id" :disabled="disableAttachmentButton" btnTitle="相关文件"></tl-attachment>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableDelApproval" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
    <!--<tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>-->
    <tlpm-risk-identify-ref ref="pmRiskIdentifyRef" @confirm="getpmRiskIdentifyRefData" :isSingleSelect="true" :externalQueryParam="{qry_state:'9'}"></tlpm-risk-identify-ref>
    <tlpm-team-ref ref="pmTeamRef" @confirm="getpmTeamRefData" :isSingleSelect="true" :externalQueryParam="{qry_projectid:model.projectid}"></tlpm-team-ref>
    <tlpm-team-ref ref="pmTeamConfirmRef" @confirm="getpmTeamRefConfirmData" :isSingleSelect="true" :externalQueryParam="{qry_projectid:model.projectid}"></tlpm-team-ref>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/refList.vue'
import tlpmRiskIdentifyRef from '@/views/ProjectManage/pmRiskIdentify/refList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlpmProjectRef, tlpmRiskIdentifyRef, tlpmTeamRef },
  data() {
    return {
      moduleTitle: '风险应对',
      urlPrefix: '/pm/riskResponses/',
      disableAttachmentButton: true,
      url: {
        // 额外的url
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        // if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
        //     this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        // }
        // this.$set(this.model, 'orgcode', this.org.code)
        // this.$set(this.model, 'orgname', this.org.name)
        // this.$set(this.model, 'deptcode', this.dept.code)
        // this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'responseusercode', this.user.code)
        this.$set(this.model, 'responseusername', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'responsetime', new Date())
      }
    },
    afterDelApproval() {
      this.disableAttachmentButton = false
    },
    loaded() {
      if (this.isAdd === false && this.disableEditSubmit === true) {
        var params = {
          sourceType: 'PmRiskResponses',
          sourceId: this.model.id
        }
        this.disableAttachmentButton = false
        getAction('/cbo/attachment/calculateCount', params).then((res) => {
          if (res.success) {
            if (res.result === 0) {
              this.disableAttachmentButton = true
              this.$emit('refresh')
            }
          }
        })
      }
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, 'billuid', this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.queryData(res.result.id)
            this.disableAttachmentButton = false
            this.isAdd = false
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    // /** 打开项目参照 */
    // openpmProjectRefDialog() {
    //     this.$refs.pmProjectRef.visible = true
    // },
    // /** 获取项目参数数据 */
    // getpmProjectRefData(selectionRow, selectKey, selectionValue) {
    //     this.$set(this.model, 'projectid', selectionRow.id)
    // },
    /** 打开风险id参照 */
    openpmRiskIdentifyRefDialog() {
      this.$refs.pmRiskIdentifyRef.visible = true
    },
    /** 获取风险id参数数据 */
    getpmRiskIdentifyRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'riskidentifyid', selectionRow.id)
      this.$set(this.model, 'riskname', selectionRow.riskname)
      this.$set(this.model, 'projectid', selectionRow.projectid)
      this.$set(this.model, 'deptcode', selectionRow.deptcode)
      this.$set(this.model, 'deptname', selectionRow.deptname)
      this.$set(this.model, 'orgcode', selectionRow.orgcode)
      this.$set(this.model, 'orgname', selectionRow.orgname)
    },
    /** 打开应对人参照 */
    openpmTeamRefDialog() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        this.$refs.pmTeamRef.visible = false
      } else {
        this.$refs.pmTeamRef.visible = true
      }
    },
    /** 获取应对人参数数据 */
    getpmTeamRefData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'responseusername', selectionRow.username)
      this.$set(this.model, 'responseusercode', selectionRow.usercode)
    },
    /** 打开确认人参照 */
    openpmTeamRefConfirmDialog() {
      if (!this.model.projectid) {
        this.$message({
          message: '请先选择项目！',
          type: 'warning'
        })
        this.$refs.pmTeamConfirmRef.visible = false
      } else {
        this.$refs.pmTeamConfirmRef.visible = true
      }
    },
    /** 获取确认人参数数据 */
    getpmTeamRefConfirmData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'confirmusername', selectionRow.username)
      this.$set(this.model, 'confirmusercode', selectionRow.usercode)
    }
  }
}
</script>

